package com.csu.backmanagesys.Controller;

import com.csu.backmanagesys.Domain.Item;
import com.csu.backmanagesys.Service.ItemService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import org.dom4j.rule.Mode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;

@RequestMapping("/Items")
@Controller
public class ItemController {
    @Autowired
    public ItemService itemService;
    @RequestMapping("")
    public ModelAndView items(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("item",itemService.selectItem());
        modelAndView.setViewName("item_manager");
        return modelAndView;
    }

    @GetMapping("/singleItem")
    public ModelAndView selectItem(ModelAndView modelAndView,String search){
        modelAndView.addObject("item",itemService.fuzzySelectItem(search));
        modelAndView.setViewName("item_manager");
        return modelAndView;
    }

    @DeleteMapping("/singleItem")
    public ModelAndView deleteItem(String itemId){
        ModelAndView modelAndView = new ModelAndView();
        itemService.deleteItem(itemId);
        modelAndView.addObject("item",itemService.selectItem());
        modelAndView.setViewName("item_manager");
        return modelAndView;
    }

    @PostMapping("/singleItem")
    public ModelAndView addNewItem(HttpServletRequest httpServletRequest)
    {
        ModelAndView modelAndView = new ModelAndView();
        String listprice = httpServletRequest.getParameter("listprice");
        String unitprice = httpServletRequest.getParameter("unitcost");
        if(listprice.length() != 0 && unitprice.length() != 0){
            BigDecimal list_price = new BigDecimal(0);
            BigDecimal unit_price = new BigDecimal(0);
            list_price = new BigDecimal(listprice);
            unit_price = new BigDecimal(unitprice);
            itemService.insertItem(
                    httpServletRequest.getParameter("itemid"),
                    httpServletRequest.getParameter("productid"),
                    list_price,
                    unit_price,
                    1,
                    "P",
                    httpServletRequest.getParameter("attr1"),
                    httpServletRequest.getParameter("attr2"),
                    httpServletRequest.getParameter("attr3"),
                    httpServletRequest.getParameter("attr4"),
                    httpServletRequest.getParameter("attr5")
            );
            itemService.insertInventory(
                    httpServletRequest.getParameter("itemid"),
                    Integer.parseInt(httpServletRequest.getParameter("qty"))
            );
        }
        modelAndView.addObject("item",itemService.selectItem());
        modelAndView.setViewName("item_manager");
        return modelAndView;
    }


    //暂时先不用这个，数据库完不成update，影响功能，先用index里面的那个
    @PostMapping("/update_item_information")
    public ModelAndView updateItem(HttpServletRequest req){
        String list_price = req.getParameter("listprice_edit");
        String unit_cost = req.getParameter("unitcost_edit");

        if (list_price!=null&unit_cost!=null) {
            String itemid = req.getParameter("itemid_eidt");
            System.out.println(itemid);
            String productid = req.getParameter("productid_eidt");
            BigDecimal listprice = new BigDecimal(list_price);
            BigDecimal unitcost = new BigDecimal(unit_cost);
            String attr1 = req.getParameter("attr1_edit");
            String attr2 = req.getParameter("attr2_edit");
            String attr3 = req.getParameter("attr3_edit");
            String attr4 = req.getParameter("attr4_edit");
            String attr5 = req.getParameter("attr5_edit");

            itemService.updateItem(itemid, productid, listprice, unitcost, 1, "p", attr1, attr2, attr3, attr4, attr5);
        }
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("item",itemService.selectItem());
        modelAndView.setViewName("item_manager");
        return modelAndView;
    }

    @GetMapping("/getItemInf")
    @ResponseBody
    public List<Item> getItemInf(HttpServletRequest req){
        String itemId = req.getParameter("itemid_edit");
        return itemService.selectItemById(itemId);
    }

    @PostMapping("/updateItemQty")
    public ModelAndView modelAndView(@RequestBody String req) throws JsonProcessingException {
        ModelAndView modelAndView = new ModelAndView();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(req);
        System.out.println(jsonNode.get("itemId").asText());
        System.out.println(jsonNode.get("stockCount").asInt());
        itemService.updateItemQty(jsonNode.get("itemId").asText() , jsonNode.get("stockCount").asInt());
        modelAndView.addObject("item",itemService.selectItem());
        modelAndView.setViewName("item_manager");
        return modelAndView;
    }
}
